﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using CH.Easy.DbProvider.Common;
namespace CH.Easy.DbProvider.ExpressionExtend
{
    public class SelectExpression : Expression
    {
        public WhereExpression Where { get; set; }
        public ColumnExpression Column { get; set; }
        public TakeExpression Take { get; set; }
        public SkipExpression Skip { get; set; }
        public OrderExpression Order { get; set; }
        public GroupExpression Group { get; set; }
        public FromExpression From { get; set; }
        public InnerJoinExpression InnerJoin { get; set; }
        public Expression Expression { get; set; }
        public bool IsHaveWhere
        {

            get
            {
                return this.Where.Expression != null;
            }

        }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public bool IsPage
        {
            get
            {
                if (this.Take.Expression != null && this.Skip.Expression != null)
                {
                    int take = (int)(((ConstantExpression)this.Take.Expression).Value);
                    int skip = (int)(((ConstantExpression)this.Skip.Expression).Value);
                    if (skip == 0 || take == 0)
                    {
                        this.PageIndex = 1;
                    }
                    else
                    {
                        this.PageIndex = (skip / take) + 1;
                    }
                    this.PageSize = take;
                    return true;

                }
                else 
                {
                    return false;
                }
            }
        }
        
        
        public SelectExpression(Expression expression)
        {
            this.Where = new WhereExpression(null);
            this.Column = new ColumnExpression(null);
            this.Take = new TakeExpression(null);
            this.Skip = new SkipExpression(null);
            this.Order = new OrderExpression();
            this.Group = new GroupExpression(null);
            this.From = new FromExpression(null);
            this.InnerJoin = new InnerJoinExpression(null);
            this.Expression = expression;
        }
        public override ExpressionType NodeType
        {
            get
            {
                return (ExpressionType)ExpressionTypeExend.Select;
            }
        }
     

    }
}
